/**
 *
 */
(function(gadgets) {

  gadgets.io = gadgets.io || {};

  var callbacks = {};
  var requestCnt= 0;
  var proxyFrameId;
  var proxyDiv;

  function initMakeRequest(params) {
    var gadgetUrl = 'http://gfcxd.googlecode.com/svn/trunk/makeRequestProxy.xml';
    /*
    var iframeUrl = 'http://ntsrovtej4m9p0mqfrf37u7a0d1amt9q.a.friendconnect.gmodules.com/gadgets/'
    google.friendconnect.container.setDomain(gadgetUrl, iframeUrl);
    */

    proxyDiv = document.createElement('div');
    proxyDiv.id = 'gfcxd-makerequest-proxy';
    proxyDiv.style.display = 'none';
    proxyDiv.style.position = 'absolute';
    proxyDiv.style.top = proxyDiv.style.left = proxyDiv.style.width = proxyDiv.style.height = '0px';
    proxyDiv.style.overflow = 'hidden';
    document.body.appendChild(proxyDiv);

    google.friendconnect.container.renderOpenSocialGadget({
      id : proxyDiv.id, url : gadgetUrl, site : params.site });

    gadgets.rpc.register('makeRequestApiReady', function(io) {
      gadgets.io.AuthorizationType = io.AuthorizationType;
      gadgets.io.ContentType = io.ContentType;
      gadgets.io.MethodType = io.MethodType;
      gadgets.io.RequestParameters = io.RequestParameters;
      params.onload();
    });

    gadgets.rpc.register('makeRequestCallback', function(reqid, response) {
      var callback = callbacks[reqid];
      callback.call(this, response);
    });
  }


  // overriding initialization
  google.friendconnect.container.initOpenSocialApi = 
    injectMakeRequestInit(google.friendconnect.container.initOpenSocialApi);
  google.friendconnect.container.loadOpenSocialApi = 
    injectMakeRequestInit(google.friendconnect.container.loadOpenSocialApi);

  function injectMakeRequestInit(fn) {
    return function(params) {
      var _this = this;
      google.setOnLoadCallback(function() {
        var _onload = params.onload;
        params.onload = function() {
          params.onload = _onload;
          fn.call(_this, params);
        }
        initMakeRequest(params);
      });
    }
  }

  /**
   *
   */
  gadgets.io.makeRequest = function(url, callback, params) {
    params = params || {};
    var reqid = 'request_' + requestCnt++;
    callbacks[reqid] = function(res) {
      if (params[gadgets.io.RequestParameters.CONTENT_TYPE]==gadgets.io.ContentType.DOM) {
        res.data = new DOMParser().parseFromString(res.text, 'application/xml');
      }
      callback.call(this, res);
    }
    if (!proxyFrameId) {
      proxyFrameId = proxyDiv.getElementsByTagName('iframe')[0].id;
    }
    gadgets.rpc.call(proxyFrameId, 'makeRequest', null, reqid, url, params);
  }

  /**
   *
  gadgets.io.getProxyUrl = function(url) { return url };
   */

  /*
  gadgets.io.encodeValues = function(fields) { return null };
  */


  /**
   * DOM Parser
   */
  var DOMParser = window.DOMParser;
  if (typeof DOMParser == "undefined") {
    DOMParser = function () {}
    DOMParser.prototype.parseFromString = function (str, contentType) {
      if (typeof ActiveXObject != "undefined") {
         var d = new ActiveXObject("MSXML.DomDocument");
         d.loadXML(str);
         return d;
      } else if (typeof XMLHttpRequest != "undefined") {
         var req = new XMLHttpRequest;
         req.open("GET", "data:" + (contentType || "application/xml") +
                         ";charset=utf-8," + encodeURIComponent(str), false);
         if (req.overrideMimeType) {
            req.overrideMimeType(contentType);
         }
         req.send(null);
         return req.responseXML;
       } else {
         return null;
       }
    }
  }

})(window.gadgets || {});
